<!--连接新增编辑-->
<template>
  <el-dialog :title="connectTitle" :visible.sync="dialogFormVisible" :show-close="false" :close-on-click-modal="false"
             width="30%">

    <el-form :model="form" :rules="rules" ref="form">

      <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
        <el-input v-model="form.name" auto-complete="off" placeholder="dms_prod"></el-input>
      </el-form-item>

      <el-form-item label="URL" :label-width="formLabelWidth" prop="url">
        <el-input v-model="form.url" auto-complete="off"
                  placeholder="jdbc:mysql://127.0.0.1:3306/scheme?useSSL=false&useUnicode=true"></el-input>
      </el-form-item>

      <el-form-item label="用户名" :label-width="formLabelWidth" prop="username">
        <el-input v-model="form.username" auto-complete="off" placeholder="root"></el-input>
      </el-form-item>

      <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
        <el-input v-model="form.password" auto-complete="off" placeholder="123456"></el-input>
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="onCancel">取 消</el-button>
      <el-button type="primary" @click="onSubmit('form')" :loading="submitLoading">
        {{ submitLoading ? '提交中 ...' : '确 定' }}
      </el-button>
      <el-button type="primary" @click="testConnection('form')" :loading="testLoading">
        {{ testLoading ? '测试中 ...' : '测 试' }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import {insertConnection, testConnection, updateConnection} from "@/api/connection-manager/ConnectionInsertAndUpdate";

export default {
  name: "DataSourceInsertAndUpdate",
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false
    },
    cancel: Function,
    form: {
      name: null,
      url: null,
      username: null,
      password: null
    },
    listRefresh: Function,
    connectTitle: String,
  },
  data() {
    return {
      formLabelWidth: '80px',
      testLoading: false,
      submitLoading: false,
      rules: {
        name: [
          {required: true, message: '请输入；连接名称', trigger: 'blur'},
          {min: 1, max: 500, message: '长度在 1 到 255 个字符', trigger: 'blur'}
        ],
        url: [
          {
            required: true,
            message: '请输入正确的URL',
            trigger: 'blur'
          },
        ],
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        password: [{required: true, message: '请输入密码', trigger: 'blur'},
        ],
      },
    }
  },
  methods: {
    openLayer(title, msg, type) {
      if (type === 'error') {
        this.$notify.error({
          title: title,
          message: msg
        });
      } else {
        this.$notify({
          title: title,
          message: msg,
          type: type
        });
      }
    },
    testConnection(formName) {
      this.testLoading = true;
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const that = this;
          const formData = that.form;
          const config = {url: formData.url, username: formData.username, password: formData.password};

          testConnection(config).then(res => {
            if (res.code === 200) {
              that.openLayer('消息', res.msg, 'success');
            } else {
              that.openLayer('消息', res.msg, 'error');
            }
            that.testLoading = false;
          })
        } else {
          this.testLoading = false;
          return false;
        }
      });
    },
    onSubmit(formName) {
      this.submitLoading = true;
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const that = this;
          const formData = that.form;
          formData.mode = 'separate';
          if (formData.connectionId === 0) {
            const config = {url: formData.url, username: formData.username, password: formData.password};
            formData.config = JSON.stringify(config);
            insertConnection(formData).then(res => {
              if (res.code === 200) {
                that.openLayer('消息', '连接新增成功', 'success');
                // 关闭弹出层
                that.onCancel();
                this.listRefresh();
              } else {
                that.openLayer('消息', res.msg, 'error');
              }
              this.submitLoading = false;
            });
          } else {
            // 更新
            const config = {url: formData.url, username: formData.username, password: formData.password};
            formData.config = JSON.stringify(config);
            updateConnection(formData).then(res => {
              if (res.code === 200) {
                that.openLayer('消息', '连接修改成功', 'success');
                // 关闭弹出层
                that.onCancel();
                that.listRefresh();
              } else {
                that.openLayer('消息', res.msg, 'error');
              }
              that.submitLoading = false;
            });
          }
        } else {
          this.submitLoading = false;
          return false;
        }
      });
    },
    onCancel() {
      this.cancel();
    },
  },
  async mounted() {

  }
}
</script>

<style scoped>

</style>